package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.aspect.LogTypeEnum;
import com.bwda.dsrs.filecenter.aspect.Loggable;
import com.bwda.dsrs.filecenter.domain.po.userstrategyrel.UserStrategyRelPo;
import com.bwda.dsrs.filecenter.domain.condition.userstrategyrel.UserStrategyRelCondition;
import com.bwda.dsrs.filecenter.domain.form.userstrategyrel.UserStrategyRelQueryForm;
import com.bwda.dsrs.filecenter.domain.form.userstrategyrel.UserStrategyRelUpdateForm;
import com.bwda.dsrs.filecenter.domain.vo.userstrategyrel.UserStrategyRelVo;
import com.bwda.dsrs.filecenter.service.UserStrategyRelService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;

import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 策略关联用户Controller层
 *
 * @author zhangyinmei
 * @date 2019年5月23日
 */
@RestController
@RequestMapping("/b/userstrategyrel")
@Api(value = "/b/userstrategyrel", tags = "用户策略关系表")
public class UserStrategyRelController extends BaseController {

	@Autowired
	private UserStrategyRelService userStrategyRelService;

	/**
	 * 查询用户策略关系表
	 * @param desensRule
	 * @return
	 * @throws BusinessServiceException
	 */
	@ApiOperation(value = "查询用户策略关系表",notes = "查询用户策略关系表",httpMethod = "GET")
	@Loggable(descp = "查询用户策略关系表",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<UserStrategyRelVo> query(@ApiParam(value = "ID", required = true)@RequestParam Long desensRule) throws BusinessServiceException {
		UserStrategyRelPo po = userStrategyRelService.queryWithValid(desensRule);
		UserStrategyRelVo vo = CopyUtil.transfer(po, UserStrategyRelVo.class);
		return getSuccessResult(vo);
	}

	/**
	 * 查询用户策略关系表数量
	 * @param form
	 * @return
	 * @throws BusinessServiceException
	 */
	@ApiOperation(value = "查询用户策略关系表数量",notes = "查询用户策略关系表数量",httpMethod = "GET")
	@Loggable(descp = "查询用户策略关系表数量",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid UserStrategyRelQueryForm form) throws BusinessServiceException {
		UserStrategyRelCondition condition = this.getConditionByQueryForm(form);
		int count = userStrategyRelService.queryCount(condition);
		return getSuccessResult(count);
	}

	/**
	 * 查询用户策略关系表列表
	 * @param form
	 * @return
	 * @throws BusinessServiceException
	 */
	@ApiOperation(value = "查询用户策略关系表列表",notes = "查询用户策略关系表列表",httpMethod = "GET")
	@Loggable(descp = "查询用户策略关系表列表",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<UserStrategyRelVo>> queryList(@ModelAttribute@Valid UserStrategyRelQueryForm form) throws BusinessServiceException {
		UserStrategyRelCondition condition = this.getConditionByQueryForm(form);
		List<UserStrategyRelPo> poList = userStrategyRelService.queryList(condition);
		List<UserStrategyRelVo> voList = CopyUtil.transfer(poList, UserStrategyRelVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	/**
	 * 查询用户策略关系表列表(带分页)
	 * @param form
	 * @return
	 * @throws BusinessServiceException
	 */
	@ApiOperation(value = "查询用户策略关系表列表(带分页)",notes = "查询用户策略关系表列表(带分页)",httpMethod = "GET")
	@Loggable(descp = "查询用户策略关系表列表(带分页)",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<UserStrategyRelVo>> queryPageList(@ModelAttribute@Valid UserStrategyRelQueryForm form) throws BusinessServiceException {
		UserStrategyRelCondition condition = this.getConditionByQueryForm(form);
		List<UserStrategyRelVo> voList = new ArrayList<>();
		int count = userStrategyRelService.queryCount(condition);
		if (count > 0) {
			List<UserStrategyRelPo> poList = userStrategyRelService.queryList(condition);
			voList = CopyUtil.transfer(poList, UserStrategyRelVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	/*@ApiOperation(value = "新增用户策略关系表",notes = "新增用户策略关系表",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<UserStrategyRelVo> add(@ModelAttribute@Valid UserStrategyRelCreateForm form) throws BusinessServiceException {
		UserStrategyRelPo po = CopyUtil.transfer(form, UserStrategyRelPo.class);
		po.setDesensRule(1L);
		userStrategyRelService.insert(po);
		UserStrategyRelVo vo = CopyUtil.transfer(po, UserStrategyRelVo.class);
		return getSuccessResult(vo);
	}*/

	/**
	 * 修改用户策略关系表
	 * @param form
	 * @return
	 * @throws BusinessServiceException
	 */
	@ApiOperation(value = "修改用户策略关系表",notes = "修改用户策略关系表",httpMethod = "POST")
	@Loggable(descp = "修改用户策略关系表",type = LogTypeEnum.UPDATE,include = "")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid UserStrategyRelUpdateForm form) throws BusinessServiceException {
		UserStrategyRelPo po = CopyUtil.transfer(form, UserStrategyRelPo.class);
		userStrategyRelService.update(po);
		return getSuccessResult();
	}

	/**
	 * 删除用户策略关系表
	 * @param desensRule
	 * @return
	 * @throws BusinessServiceException
	 */
	@ApiOperation(value = "删除用户策略关系表",notes = "删除用户策略关系表",httpMethod = "POST")
	@Loggable(descp = "删除用户策略关系表",type = LogTypeEnum.DELETE,include = "")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "ID", required = true)@RequestParam Long desensRule) throws BusinessServiceException {
		userStrategyRelService.delete(desensRule);
		return getSuccessResult();
	}

	/**
	 * UserStrategyRelQueryForm转换为UserStrategyRelCondition
	 *
	 * @param form
	 * @return
	 */
	private UserStrategyRelCondition getConditionByQueryForm(UserStrategyRelQueryForm form) {
		UserStrategyRelCondition condition = CopyUtil.transfer(form, UserStrategyRelCondition.class);
		return condition;
	}

}